<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-1-24
 * Time: 下午5:34
 */

namespace common\models\credit_app;

use common\components\tools\Tools;
use common\models\CreditOrder;
use yii\base\ErrorException;
use yii\helpers\ArrayHelper;

/**
 * Class OrderCert
 * @package common\models\credit_app
 *
 * @property Order $order
 */
class OrderCert extends \common\models\credit_app_tables\OrderCert
{
    const KEY_VIP = 'vip';
    const KEY_SPOUSE = 'spouse';
    const KEY_GUARANTOR = 'guarantor';
    const STATUS_OK = 'ok';

    public static $serialize_fields = [
        'ct_attach',
        'ct_sign',
        'ct_video',
        'ct_detail',
    ];
    public static function getKey()
    {}
    public static function getKeyRelation()
    {
        return [
            self::KEY_VIP => CreditOrder::RELATION_SELF,
            self::KEY_SPOUSE => CreditOrder::RELATION_POUSE,
            self::KEY_GUARANTOR => CreditOrder::RELATION_GUARANTEE,
        ];
    }

    /**
     * @param $id
     * @return null|static
     */
    public static function findActiveOneById($id)
    {
        return static::findOne(['ct_id'=>$id]);
    }

    /**
     * @param $id
     * @return null|static
     */
    public static function findActiveOneGuarantorById($id)
    {
        return static::findOne(['ct_id'=>$id, 'ct_key'=>self::KEY_GUARANTOR]);
    }

    public function rules()
    {
        return [
//            [['ct_oid', 'ct_attach', 'ct_name', 'ct_code', 'ct_sex', 'ct_birth', 'ct_affiliation', 'ct_idaddress', 'ct_validity_start', 'ct_validity_end', 'ct_remark', 'ct_detail', 'ct_dateline', 'ct_querytime'], 'required'],
            [['ct_oid', 'ct_dateline', 'ct_skip', 'ct_querytime'], 'integer'],
            [['ct_key', 'ct_attach', 'ct_sign', 'ct_video', 'ct_sex', 'ct_remark', 'ct_detail', 'ct_results', 'ct_status', 'ct_guarantor_type'], 'string'],
            [['ct_birth', 'ct_validity_start', 'ct_validity_end'], 'safe'],
            [['ct_name', 'ct_code'], 'string', 'max' => 30],
            [['ct_affiliation', 'ct_idaddress'], 'string', 'max' => 50],
            [['ct_issue'], 'string', 'max' => 100],
            [['ct_mobile'], 'string', 'max' => 20],
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getOrder()
    {
        return $this->hasOne(Order::className(), ['o_id' => 'ct_oid']);
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        $_ks = ['ct_sign'=>'oa_sign', 'ct_video'=>'oa_video'];
        $order = $this->order;
        if (!$order){
            throw new ErrorException("没有找到该订单，order_id:{$this->order->o_id}");
        }
        $attachment = $this->order->attachment;
        if (!$attachment){
//            throw new ErrorException("没有找到附件信息，order_id:{$this->order->o_id}");
            $attachment = new OrderAttachment();
            $attachment->oa_oid = $this->order->o_id;
        }
        foreach ($_ks as $k => $v){
            if (array_key_exists($k, $changedAttributes)){
                $x = [];
                foreach ($order->certs as $k1 => $v1){
                    if ($v1->$k)
                    $x = ArrayHelper::merge($x, unserialize($v1->$k));
                }
                $attachment->$v = serialize($x);
            }
        }
        $attachment->save(false);
    }
}